﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DTO;
using System.Collections.Generic;
using HRProject.Code.Common;

namespace HRProject.Code.DAO
{
    public class AttachmentDAO : BaseDAO
    {
        public AttachmentDAO()
            : base ()
        {
        }

        public List<BLAttachment> Search(BLAttachment filter)
        {
            List<BLAttachment> result = new List<BLAttachment>();
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@id", filter.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@contentId", filter.ContentId, ParameterDirection.Input));
            DataTable data = select("usp_SearchAttachments", condition);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                BLAttachment file = new BLAttachment();
                file.Id = (Guid)data.Rows[i]["AttachmentId"];
                file.Title = (string)data.Rows[i]["Title"];
                file.ContentId = (Guid)data.Rows[i]["ContentId"];
                file.Url = (string)data.Rows[i]["Url"];
                file.UpdateDate = (DateTime)data.Rows[i]["UpdateDate"];
                file.UpdateUserId = (Guid)data.Rows[i]["UpdateUserId"];
                file.DataSignature = (long)data.Rows[i]["DataSignature"];
                result.Add(file);
            }
            return result;
        }
        
        public int Delete(BLAttachment attachment, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@id", attachment.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", attachment.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_DeleteAttachment", condition);
            errorCode = (int)condition[condition.Count - 1].ParamValue;
            return result;
        }

        public int Insert(BLAttachment attachment, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@id", attachment.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@title", attachment.Title, ParameterDirection.Input));
            condition.Add(new BaseCondition("@contentId", attachment.ContentId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@description", attachment.Url, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", attachment.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_AddAttachment", condition);
            errorCode = (int)condition[condition.Count - 1].ParamValue;
            return result;
        }
    }
}
